package com.df.bpm.utils.businessViewerV2.business;

import com.df.bpm.utils.businessViewerV2.Main;
import com.df.bpm.utils.businessViewerV2.entity.C_entity;
import com.df.bpm.utils.businessViewerV2.entity.M_entityDto;
import com.df.bpm.utils.businessViewerV2.res.V_scrollPanel;
import com.df.common.utils.CommonUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class V_scroll4BusinessDto extends V_scrollPanel<M_businessDto> {


    Integer requirementId;


    Integer entityId4Output;

    V_businessByOutput baba;

    public V_scroll4BusinessDto(Integer requirementId,Integer entityId4Output,V_businessByOutput baba){
        this.baba=baba;
        this.requirementId=requirementId;
        this.entityId4Output=entityId4Output;
        List<M_businessDto> mBusinessDto=C_business.getBusinessDto(entityId4Output);
        this.putModel(mBusinessDto);
    }

    public void refresh(){
        List<M_businessDto> mBusinessDto=C_business.getBusinessDto(entityId4Output);
        this.putModel(mBusinessDto);
    }

    @Override
    public List<String> getColNames() {
        return Arrays.asList("业务ID", "序号","包含目标输出的业务","实体ID","担当者","创建时间","更新时间");
    }

    @Override
    public void hideCol(JTable iTable) {
        Main.hideCol(iTable,0);
        Main.hideCol(iTable,3);
    }

    @Override
    public List<Integer> colWidthSetting() {
        return new ArrayList<>(Arrays.asList(1,3,11,1,5,11,11));
    }

    @Override
    public Object[] getRowValues(M_businessDto row) {
        return new Object[]{row.getBusinessInformationIndex(),-1,row.getBusinessDesc(),
                row.getEntityID(),row.getEntityName(),
                dateFormat.format(row.getCreateDate()),
                dateFormat.format(row.getUpdateDate())};
    }

    @Override
    public M_businessDto getEmptyRow() {
        return new M_businessDto();
    }

    @Override
    public MouseListener mouseAdapter4Table(JTable iTable, List<M_businessDto> list) {
        return new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = iTable.rowAtPoint(e.getPoint());
                int col = iTable.columnAtPoint(e.getPoint());
                if (row >= 0 ) {
                    if (e.getClickCount() == 2) {// 处理双击事件
                        if((col==2)){//编辑业务名称
                            Object oldValue=iTable.getValueAt(row, col);
                            String tips="请输入业务名称：";
                            String newValue= (String) JOptionPane.showInputDialog(null, tips, "名称", JOptionPane.PLAIN_MESSAGE, null, null, oldValue);
                            if(newValue!=null && !newValue.isEmpty() && !newValue.equals(oldValue)){
                                list.get(row).setBusinessDesc(newValue);
                                iTable.setValueAt(newValue, row, col);
                                if(row==list.size()-1){
                                    list.add(getEmptyRow());
                                    ((DefaultTableModel)iTable.getModel()).addRow(new Object[getColNames().size()]);
                                }
                                if(list.get(row).getBusinessInformationIndex()!=null){
                                    if(newValue.equals("-")){//删
                                        C_business.deleteBusiness(list.get(row).getBusinessInformationIndex());
                                        refresh();
                                    }else{//改
                                        C_business.updateBusiness(list.get(row),requirementId);
                                        refresh();
                                    }
                                }else{//增
                                    if(list.get(row).getEntityID()!=null ){
                                        C_business.addBusiness(list.get(row),requirementId,entityId4Output);
                                        refresh();
                                    }
                                }

                            }
                        }else if((col==4)){// 更换业务担当者
                            Object oldValue=iTable.getValueAt(row, 3);
                            M_entityDto mEntityDto = C_entity.selectOne();
                            if(CommonUtils.varIsNotBlank(mEntityDto)
                                    && !mEntityDto.getBusinessEntityIndex().equals(oldValue)){
                                list.get(row).setEntityID(mEntityDto.getBusinessEntityIndex());
                                list.get(row).setEntityName(mEntityDto.getName());
                                iTable.setValueAt(mEntityDto.getBusinessEntityIndex(), row, 3);
                                iTable.setValueAt(mEntityDto.getName(), row, 4);
                                if(row==list.size()-1){
                                    list.add(getEmptyRow());
                                    ((DefaultTableModel)iTable.getModel()).addRow(new Object[getColNames().size()]);
                                }
                                if(list.get(row).getBusinessInformationIndex()!=null){
                                    C_business.updateBusiness(list.get(row),requirementId);
                                }else{//增
                                    if(list.get(row).getBusinessDesc()!=null
                                            && !list.get(row).getBusinessDesc().equals("-")){
                                        C_business.addBusiness(list.get(row),requirementId,entityId4Output);
                                        refresh();
                                    }
                                }
                            }
                        }else if(col==1){// 查询业务详情
                            M_businessDto temp=list.get(row);
                            if(temp!=null) {
                                baba.setActiveBusiness(temp.getBusinessInformationIndex(), temp.getBusinessDesc());
                                baba.vHistory.reset();
                                baba.vHistory.insert(temp.getBusinessInformationIndex(), temp.getBusinessDesc());
                            }
                        }
                    }else if (e.getClickCount() == 1){
                        M_businessDto temp=list.get(row);
                        if(temp!=null){
                            baba.showBusinessInputOutput(temp.getBusinessInformationIndex());
                        }
                    }
                }
            }
        };
    }

}
